package site.kitcheng.springboottest.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import site.kitcheng.springboottest.app.service.ISysProfileService;
import site.kitcheng.springboottest.domain.SysProfile;
import site.kitcheng.springboottest.domain.SysProfileValue;
import site.kitcheng.springboottest.domain.vo.SysProfileVo;
import site.kitcheng.springboottest.infra.constant.BaseConstant;
import site.kitcheng.springboottest.infra.mapper.SysProfileMapper;
import site.kitcheng.springboottest.infra.mapper.SysProfileValueMapper;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-20
 */
@Service
@Slf4j
public class SysProfileServiceImpl extends ServiceImpl<SysProfileMapper, SysProfile> implements ISysProfileService {


    @Autowired
    private SysProfileValueMapper sysProfileValueMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public SysProfileVo getProfileObject(String profileCode) {

        if (StringUtils.isEmpty(profileCode)){
            return null;
        }
        String RedisKey = BaseConstant.BaseConstentProfile.REIDIS_PROFILE_CODE+profileCode;

        String s = stringRedisTemplate.opsForValue().get(RedisKey);
        if (StringUtils.isNotEmpty(s)){
            log.debug("redis缓存命中:{}", s);
            return JSON.parseObject(s, SysProfileVo.class);
        }

        SysProfile sysProfile = baseMapper.selectOne(new LambdaQueryWrapper<SysProfile>()
                .eq(SysProfile::getProfileName, profileCode));
        SysProfileVo sysProfileVo = new SysProfileVo();
        if (BeanUtil.isNotEmpty(sysProfile)) {
            BeanUtil.copyProperties(sysProfile, sysProfileVo);
            List<SysProfileValue> sysProfileValues = sysProfileValueMapper.selectList(new LambdaQueryWrapper<SysProfileValue>()
                    .eq(SysProfileValue::getProfileId, sysProfile.getId())
            );
            sysProfileVo.setSysProfileValueList(sysProfileValues);
        }
        stringRedisTemplate.opsForValue().set(RedisKey, JSON.toJSONString(sysProfileVo));
        return sysProfileVo;
    }
    @Override
    public String getProfileCode(String profileCode, Long userId, Long roleId) {
        SysProfileVo profileObject = getProfileObject(profileCode);
        String userKey = null;
        String roleKey = null;
        String globalKey = null;
        if (BeanUtil.isNotEmpty(profileObject)){
            List<SysProfileValue> sysProfileValueList = profileObject.getSysProfileValueList();
            if (CollectionUtil.isNotEmpty(sysProfileValueList)){
                if (userId!= null) {
                    List<SysProfileValue> user = sysProfileValueList.stream().filter(item ->BaseConstant.BaseConstentProfile.PROFILE_USER.equals( item.getLevelCode())).collect(Collectors.toList());
                    userKey = user.stream()
                            .filter(item -> String.valueOf(userId).equals(item.getLevelValue()))
                            .map(SysProfileValue::getValue)
                            .findFirst()
                            .orElse(null);
                }
                if (roleId!= null) {
                    List<SysProfileValue> Role = sysProfileValueList.stream().filter(item ->BaseConstant.BaseConstentProfile.PROFILE_ROLE.equals( item.getLevelCode())).collect(Collectors.toList());
                    roleKey = Role.stream()
                            .filter(item -> String.valueOf(roleId).equals(item.getLevelValue()))
                            .map(SysProfileValue::getValue)
                            .findFirst()
                            .orElse(null);
                }
                List<SysProfileValue> global = sysProfileValueList.stream().filter(item ->BaseConstant.BaseConstentProfile.PROFILE_GLOBAL.equals(item.getLevelCode())).collect(Collectors.toList());
                globalKey = global.stream()
                        .map(SysProfileValue::getValue)
                        .findFirst()
                        .orElse(null);
            }
            if (StringUtils.isNotEmpty(userKey)) {
                return userKey;
            } else if (StringUtils.isNotEmpty(roleKey)) {
                return roleKey;
            } else if (StringUtils.isNotEmpty(globalKey)) {
                return globalKey;
            }
        }
        return null;
    }
}
